package com.tbit.uqbike.quartz;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.constant.ActivityConstant;
import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.ActivityMsg;
import com.tbit.uqbike.object.pojo.BalanceStat;
import com.tbit.uqbike.service.rpcservice.StatService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.ActivityDao;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 活动定时任务
 *
 * @Author:BUGTian
 * @DATE: 2022/5/18
 */
@Component
@Slf4j
public class ActivityQuartz {

    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private StatService statService;


    /**
     * 更新活动状态
     */
    //@Scheduled(cron = "0/5 * * * * ? ")
    public void updateActivityStateQuartz() {
        List<ActivityMsg> activityMsgList = activityDao.selectActivityMsg();
        if (activityMsgList == null || activityMsgList.size() < 1) {
            //不存在开启活动数据就不需要维护活动状态了
            return;
        }
        long now = System.currentTimeMillis();
        //需要更新状态的活动
        List<ActivityMsg> updateActivityList = new ArrayList<>();
        for (ActivityMsg activityMsg : activityMsgList) {
            long startTime = activityMsg.getStartTime().getTime();
            long expireTime = activityMsg.getExpireTime().getTime();
            switch (activityMsg.getIsvalid()) {
                case 0:
                    //未开始
                    if (startTime < now && expireTime > now) {
                        activityMsg.setIsvalid(ActivityConstant.START);
                        updateActivityList.add(activityMsg);
                    }
                    break;
                case 1:
                    //进行中
                    if (expireTime < now) {
                        activityMsg.setIsvalid(ActivityConstant.END);
                        //结束的活动自动关闭开关
                        activityMsg.setState(ActivityConstant.CLOSE);
                        updateActivityList.add(activityMsg);
                    }
                    break;
                default:
                    break;
            }
            if (updateActivityList.size() > 0) {
                for (ActivityMsg msg : updateActivityList) {
                    //更新状态
                    activityDao.updateState(msg.getInviteId(), msg.getIsvalid(), msg.getState());
                }
            }
        }
    }

    /**
     * 每天凌晨定时统计用户余额信息
     * 2023-05-12 00:00:01
     */
    @Scheduled(cron = "1 0 0 * * ?")//0
    public void balanceStat() {
        log.info("定时统计各品牌用户余额信息开始");
        String today = DateUtil.today();
        String url = OperaPswConstant.PATH + "balanceStat/";
        File folder = new File(url);
        //第一次导出需要创建该文件夹
        if (!folder.exists() && !folder.isDirectory()) {
            boolean res = folder.mkdirs();
            if (res) {
                log.info("创建文件夹成功");
            } else {
                log.info("创建文件夹失败");
            }
        }
        //获取所有品牌
        List<Account> accountList = accountDao.getAllAccount(1);
        List<BalanceStat> list = new LinkedList<>();
        for (Account account : accountList) {
            JSONObject jsonObject = statService.statByBid(account.getAccountId());
            list.add(new BalanceStat(account.getName(),
                    today,
                    jsonObject.getString("machineTotal"),
                    jsonObject.getString("onlineMachineTotal"),
                    jsonObject.getString("userTotal"),
                    jsonObject.getString("depositTotal"),
                    jsonObject.getString("userTotalMoney"),
                    jsonObject.getString("depositMoney")
            ));
        }
        ExcelUtil<BalanceStat> excelUtil = new ExcelUtil<>(BalanceStat.class);
        excelUtil.exportExcel(list, today + "用户余额信息", url);
        log.info("定时统计各品牌用户余额信息结束");
    }

}
